<?php

/*
 * Preguntas
 * Listado de preguntas
 * ABM de preguntas
*/

class Preguntas extends Controller {

    function Preguntas() {
        parent::Controller();
        $this->load->library('session');
        if(!$this->session->userdata('logged_in'))
            redirect('/home/');
        $this->load->language('general',$this->session->userdata('lenguaje'));
    }

    function index() {
        $datos['titulo_pagina'] = 'Preguntas';
        $this->load->view('admin/encabezado_admin.php', $datos);
        $this->load->view('admin/menu_admin.php');
        $this->load->view('admin/preguntas.php');
        $this->load->view('admin/pie_admin.php');
    }

    //preguntas
    function post() {
        $tabla       = "productos_preguntas";
        $tabla1      = "productos_respuestas";
        $page        = $this->input->post('page');
        $limit       = $this->input->post('rows');
        $sidx        = $this->input->post('sidx');
        $sord        = $this->input->post('sord');
        $_search     = $this->input->post('_search');
        $searchField = $this->input->post('searchField');
        $searchString= $this->input->post('searchString');

        $where = "";
        if ($_search=='true')
            $where = " WHERE $searchField LIKE '%$searchString%' ";


        if(!$sidx) $sidx =1;

        $sql    = "SELECT * FROM $tabla $where";
        //echo $sql;
        $query  = $this->db->query($sql);
        $result = $query->result();

        $count  = count($result);

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $sql    = "SELECT * FROM $tabla $where ORDER BY $sidx $sord LIMIT $start , $limit";
        $query  = $this->db->query($sql);

        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        $i=0;
        $data['page']= $page;
        $data['total']= $total_pages;

        foreach ($query->result() as $row) {
            $data['rows'][$i]['id']= $row->id_pregunta;
            $data['rows'][$i]['cell'][]= $row->id_pregunta;
            $data['rows'][$i]['cell'][]= utf8_encode($row->id_usuario);
            $data['rows'][$i]['cell'][]= utf8_encode($row->id_vendedor);
            $data['rows'][$i]['cell'][]= utf8_encode($row->id_producto);
            $data['rows'][$i]['cell'][]= utf8_encode('Pregunta');
            $data['rows'][$i]['cell'][]= utf8_encode($row->diahora);
            $data['rows'][$i]['cell'][]= $row->texto;
            $data['rows'][$i]['cell'][]= $row->pendiente;
            $i++;
        }
        echo json_encode($data);
    }

    function guardar() {
        $tabla    = 'productos_preguntas';
        $texto       = $this->input->post('texto');
        $id_usuario  = $this->input->post('id_usuario');
        $id_vendedor = $this->input->post('id_vendedor');
        $id_producto = $this->input->post('id_producto');
        $id       = $this->input->post('id');
        $pendiente= $this->input->post('pendiente');
        //$query = $this->db->query("UPDATE $tabla set pendiente='".$pendiente."' WHERE id_pregunta=".$id);

        $query = $this->db->query("UPDATE $tabla set pendiente='".$pendiente."', id_usuario='".$id_usuario."', id_vendedor='".$id_vendedor."', id_producto='".$id_producto."', diahora='".$diahora."', texto='".$texto."' WHERE id_pregunta=".$id);
       }

    //respuestas
    function post1() {
        $tabla       = "productos_respuestas";
        $page        = $this->input->post('page');
        $limit       = $this->input->post('rows');
        $sidx        = $this->input->post('sidx');
        $sord        = $this->input->post('sord');
        $_search     = $this->input->post('_search');
        $searchField = $this->input->post('searchField');
        $searchString= $this->input->post('searchString');
        $id = $this->uri->segment(4);

        $where = "";
        if ($_search=='true')
            $where = " WHERE $searchField LIKE '%$searchString%' ";


        if(!$sidx) $sidx =1;

        $sql    = "SELECT * FROM $tabla $where";
        //echo $sql;
        $query  = $this->db->query($sql);
        $result = $query->result();

        $count  = count($result);

        if( $count > 0 && $limit > 0) {
            $total_pages = ceil($count/$limit);
        } else {
            $total_pages = 0;
        }

        if ($page > $total_pages) $page=$total_pages;
        $start = $limit*$page - $limit;
        if($start <0) $start = 0;

        $sql    = "SELECT * FROM $tabla WHERE id_pregunta=".$id;
        $query  = $this->db->query($sql);

        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT" );
        header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT" );
        header("Cache-Control: no-cache, must-revalidate" );
        header("Pragma: no-cache" );
        header("Content-type: text/x-json");
        $i=0;
        $data['page']= $page;
        $data['total']= $total_pages;

        foreach ($query->result() as $row) {
            $data['rows'][$i]['id_pregunta']= $row->id_pregunta;
            $data['rows'][$i]['cell'][]= $row->id_pregunta;
            $data['rows'][$i]['cell'][]= utf8_encode($row->id_usuario);
            $data['rows'][$i]['cell'][]= utf8_encode($row->id_vendedor);
            $data['rows'][$i]['cell'][]= utf8_encode($row->id_producto);
            $data['rows'][$i]['cell'][]= utf8_encode('Pregunta');
            $data['rows'][$i]['cell'][]= utf8_encode($row->diahora);
            $data['rows'][$i]['cell'][]= utf8_encode($row->texto);
            $i++;
        }
        echo json_encode($data);
    }

    function guardar1() {
        $tabla       = "productos_respuestas";
        $tabla1      = "productos_preguntas";
        $oper        = $this->input->post('oper');
        $id_pregunta = $this->uri->segment(4);

        $id          = $this->input->post('id');
        $texto       = $this->input->post('texto');
        $id_usuario  = $this->input->post('id_usuario');
        $id_vendedor = $this->input->post('id_vendedor');
        $id_producto = $this->input->post('id_producto');

        $diahora     = date('Y-m-d H:m:s');

        if($oper=='edit') {
            $query = $this->db->query("UPDATE $tabla set id_usuario='".$id_usuario."', id_vendedor='".$id_vendedor."', id_producto='".$id_producto."', diahora='".$diahora."', texto='".$texto."' WHERE id_pregunta=".$id_pregunta);
        }elseif($oper=='add') {
            $sql    = "SELECT * FROM $tabla1 WHERE id_pregunta=".$id_pregunta;
            $query  = $this->db->query($sql);
            $row    = $query->row();
            $id_usuario  = $row->id_usuario;
            $id_vendedor = $row->id_vendedor;
            $id_producto = $row->id_producto;
            
            $query = $this->db->query("INSERT INTO $tabla set id_pregunta='".$id_pregunta."', id_usuario='".$id_usuario."', id_vendedor='".$id_vendedor."', id_producto='".$id_producto."', diahora='".$diahora."', texto='".$texto."'");
            echo $this->db->last_query();
        }elseif($oper=='del') {
            $query = $this->db->query("DELETE FROM $tabla WHERE id=$id");
        }
    }


}
?>